
//
//  ZMsgViewController.m
//  ZQLearn
//
//  Created by 朵 on 2019/4/22.
//  Copyright © 2019年 macfeel. All rights reserved.
//
#import "ZMsgDetailViewController.h"
#import "ZMsgViewController.h"
#import "CommonModel.h"
#import "ScoreCell.h"
@interface ZMsgViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
///刷新和加载更多时候用
@property (nonatomic,assign)NSInteger currentPage;


@end

@implementation ZMsgViewController

#define cellIdentifier @"score"
////表样板
-(UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight -NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 66;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"ScoreCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        
        WeakStament(ws);
        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
            [ws loadData];
        }];
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            [ws loadMoreData];
//        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
     [self.navigationItem setItemWithTitle:@"消息" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.dataArray = [NSMutableArray array];
    self.currentPage = 1;
    [self.view addSubview:self.tableView];
    [self loadData];
//     [self showNoDataImageView:@"none" withTitle:@"暂无数据哦"];
    // Do any additional setup after loading the view.
}

-(void)loadData{
    
    if (self.currentPage==1) {
        [self.dataArray removeAllObjects];
    }
    NSDictionary *dic = @{@"userInfo.id":[UserInfoSave shareInstance].uid?:@"",@"pageSize":@(60),@"pageIndex":@(self.currentPage)};
    [ZNetworkService getRequestWithBaseUrl:kMsgUrl setParameters:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                CommonModel *model = [CommonModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
                
            }
            [self.tableView reloadData];
            [self endRefresh];
            if (arr.count==0) {
                [self noMoreData];
            }
        }
    } fail:^(NSError *error) {
        [self endRefresh];
    } delegater:nil];
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonModel *model = self.dataArray[indexPath.row];
    ZMsgDetailViewController *det = [ZMsgDetailViewController new];
    det.cid = model.ID;
    det.pushtime = model.pushTime;
    det.ttitle = model.title;
    [self.navigationController pushViewController:det animated:YES];
}

#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonModel *model = self.dataArray[indexPath.row];
    ScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.timeLab.text = model.pushTime;
//    cell.jifenLab.text = [NSString stringWithFormat:@"+%@",model.integral];
    cell.nameLab.text = model.title;
    cell.headImg.image = WDImgName(@"ring");
    cell.jifenLab.hidden = YES;
    return cell;
}



- (void)loadMoreData{
    self.currentPage++;
    [self loadData];
}


- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}


///没有更多数据
- (void)noMoreData {
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}


@end
